﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using ISAMonitor.DNSPODAPI;
using ISAMonitor.RESTTool;

namespace ISAMonitor.DNSPODAPI
{
    /// <summary>
    /// 对DNSPOD的API进行封装
    /// </summary>
    public class DNSPodCommand
    {
        private readonly string username;
        private readonly string password;

        private string CommandInvoke(string pagename,string commandparam)
        {
            /*login_email - 用户帐号
 login_password - 用户密码
              format {json,xml} - 返回的数据格式，支持json和xml.*/


            StringBuilder mysb = new StringBuilder();
            mysb.Append("login_email=" + username +"&");
            mysb.Append("login_password=" + password + "&");
            mysb.Append("format=xml"+"&");
            mysb.Append(commandparam);


            string ret = RESTCommand.SendCommand("https://dnsapi.cn"+pagename,mysb.ToString());
            return ret;
        }

        public DNSPodCommand(string username, string password)
        {
            this.username = username;
            this.password = password;
        }
        #region 域名操作
        /// <summary>
        /// 增加一个域名，到对应的帐号
        /// </summary>
        /// <param name="domain_name"></param>
        /// <returns></returns>
        public string AddDomain(string domain_name)
        {
            return CommandInvoke(@"/Domain.Create", "domain=" + domain_name);
        }
       
        /// <summary>
        /// 获取域名列表
        /// </summary>
        /// <returns></returns>
        public string GetDomainList()
        {
            return CommandInvoke(@"/Domain.List", "type=mine&offset=0&length=6&user_id=0");
        }
        /// <summary>
        /// 册除一个域名
        /// </summary>
        /// <param name="domain_id">域名的ID</param>
        /// <returns></returns>

        public string RemoveDomain(string domain_id)
        {
            return CommandInvoke(@"/Domain.Remove", "domain_id=" + domain_id);
        }

        /// <summary>
        /// 设置状态
        /// </summary>
        /// <param name="enabled"></param>
        /// <returns></returns>
        public string SetDomainStatus(bool enabled)
        {
            string status = enabled ? "enable" : "disable";
            return CommandInvoke(@"/Domain.Remove", "status=" + status);
        }
        #endregion

        #region 记录操作
        //domain_id - 域名ID, 如 12345
        //sub_domain - 主机记录, 如 www
        //record_type {'A','CNAME','MX','URL','NS','TXT','AAAA'} - 记录类型
        //record_line 
        public string AddRecord(string domain_id,string sub_domain,string record_type,string record_line,string value,string mx,string ttl )
        {
            string cmdstr = "domain_id=" + domain_id;
            cmdstr += "&sub_domain=" + sub_domain;
            cmdstr += "&record_type=" + record_type;
            cmdstr += "&record_line=" + record_line;
            cmdstr += "&value=" + value;
            cmdstr += "&mx=" + mx;
            cmdstr += "&ttl=" +ttl;
            //System.Windows.Forms.MessageBox.Show(cmdstr);
            return CommandInvoke(@"/Record.Create",cmdstr);
        }
        /// <summary>
        /// 
        /// </summary>
        /// domain_id - 域名ID, 如 12345
        /// record_id - 记录ID, 如 1234567
        /// <param name="domain_id"></param>
        /// <param name="record_id"></param>
        /// <returns></returns>
        public string RemoveRecord(string domain_id, string record_id)
        {
            string cmdstr = "domain_id=" + domain_id;
            cmdstr += "&record_id=" + record_id;
            return CommandInvoke(@"/Record.Remove", cmdstr);
        }
        /// <summary>
        /// domain_id - 域名ID, 如 12345
        /// record_id - 记录ID, 如 1234567
        /// status {enable,disable} - 状态
        /// </summary>
        /// <returns></returns>
        public string SetRecordStatus(string domain_id,string record_id,bool enabled)
        {
            string status = enabled ? "enable" : "disable";
            string cmdstr = "domain_id=" + domain_id;
            cmdstr += "&record_id=" + record_id;
            cmdstr += "&status=" + status;
            return CommandInvoke(@"/Record.Create", cmdstr);
        }
        
        public string GetRecordList(string domain_id)
        {
            return CommandInvoke(@"/Record.List", "domain_id=" + domain_id+"&offset=0&length=6");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="domain_id">域名ID, 如 12345</param>
        /// <param name="record_id">记录ID, 如 1234567</param>
        /// <param name="sub_domain"></param>
        /// <param name="record_type"> {'A','CNAME','MX','URL','NS','TXT','AAAA'} - 记录类型</param>
        /// <param name="record_line"> {'default', 'tel', 'cnc', 'edu', 'cmc', 'foreign'} - 记录线路依次为：默认、电信、网通、教育网、移动、国外（红字仅对VIP有效）</param>
        /// <param name="value">记录值, 如 IP:200.200.200.200, CNAME: cname.dnspod.com., MX: mail.dnspod.com</param>
        /// <param name="mx">MX优先级, 当记录类型是 MX 时有效，范围1-20</param>
        /// <param name="ttl">ttl {1-604800} - TTL，范围1-604800</param>
        /// <returns></returns>
        public string ModifyRecord(string domain_id, string record_id, string sub_domain, string record_type, string record_line, string value, string mx, string ttl)
        {
            //TODO 等待奶罩修改API实现后继续！！！
            StringBuilder mysb = new StringBuilder();
            mysb.Append("domain_id=" + domain_id + "&");
            mysb.Append("record_id=" + record_id + "&");
            mysb.Append("sub_domain=" + sub_domain + "&");
            mysb.Append("record_type=" + record_type + "&");
            mysb.Append("record_line=" + record_line + "&");
            mysb.Append("value=" + value + "&");
            mysb.Append("mx=" + mx + "&");
            mysb.Append("ttl=" + ttl + "&");

            return CommandInvoke(@"/Record.Modify", mysb.ToString());

        }
        #endregion
    }
}